package org.tity.test.java;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.tity.commons.log.Logger;

import com.alibaba.fastjson.JSONObject;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @ClassName WeChatMessageInterface
 * @Description 微信消息接口
 * @Author hao.wen@hand-china.com
 * @Date 2019/10/10 14:15
 * @Version 1.0
 **/
 
public class WeChatMessageInterface{

    @Value("${wechat.url}")
    private String url="https://open.auxgroup.com/api/wechat/message/send/text";
    @Value("${wechat.agent-id}")
    private String agentId;

   
    
    public static void main(String[] args) throws IOException {
		(new WeChatMessageInterface()).sendWechatMessage(null);
	}

    /**
     * 调用微信发消息接口
     * @param content
     */
    private void sendWechatMessage(String content) throws IOException {
    	OkHttpClient client = new OkHttpClient();
		RequestBody requestBody = null;
		MediaType mediaType= MediaType.parse("application/json; charset=utf-8");
		
		//content里面是:{"agentId":"9","content":"您提交的关于【11测】的合同申请流程已审批通过，请在1天内用章。","userIds":["yanhaibiao"]}
		
		Map<String,Object> params=new HashMap<String, Object>();
		params.put("agentId", 9);
		params.put("content", "您提交的关于【11测】的合同申请流程已审批通过，请在1天内用章。");
		params.put("userIds", Arrays.asList("yanhaibiao"));
		try {
			requestBody = FormBody.create(mediaType, JSONObject.toJSONString(params));
			Request request = new Request.Builder().url(url).post(requestBody).build();
			Response response = client.newCall(request).execute();
			String jsonstr=response.body().string();
			System.out.println(jsonstr);
			if(jsonstr==null || jsonstr.length()==0 || jsonstr.startsWith("<"))
			{
				Logger.error("服务调用异常");
				Logger.error(url);
 
			}
			else
			{
				//return jsonstr;
			}
		}catch(Exception e) {
			Logger.exception("服务调用异常",e);
			Logger.error(url);
			//return null;
		}
    }
}
